﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using KAImage;
using KAImage.Config;

namespace IPCPhotoEditor
{
    public partial class FormMerging : Form
    {
        private ImageMegreOption _option = null;
        public ImageMegreOption Option
        {
            get { return _option; }
        }

        private int _selectMode = 0;

        /// <summary>
        /// 滤色合成混合模式的不透明度
        /// </summary>
        private int _opacityScreen = 0;
        public int OpacityScreen
        {
            get
            {
                return _opacityScreen;
            }
            set
            {
                if (_opacityScreen != value)
                {
                    _opacityScreen = value;
                    if (!this.InvokeRequired)
                    {
                        if (textBoxOpacityScreen != null)
                        {
                            textBoxOpacityScreen.Text = value.ToString();
                        }
                        if (trackBarOpacityScreen != null)
                        {
                            trackBarOpacityScreen.Value = value;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 正片叠底混合模式的不透明度
        /// </summary>
        private int _opacityMultiply = 0;
        public int OpacityMultiply
        {
            get
            {
                return _opacityMultiply;
            }
            set
            {
                if (_opacityMultiply != value)
                {
                    _opacityMultiply = value;
                    if (!this.InvokeRequired)
                    {
                        if (textBoxOpacityMultiply != null)
                        {
                            textBoxOpacityMultiply.Text = value.ToString();
                        }
                        if (trackBarOpacityMultiply != null)
                        {
                            trackBarOpacityMultiply.Value = value;
                        }
                    }
                }
            }
        }
 
        private Bitmap _sourceImage = null;

        private Bitmap _destImage = null;

        public WorkImageUpdatedEventHandler WorkImageUpdated = null;

        protected void OnWorkImageUpdated(Bitmap image)
        {
            if (WorkImageUpdated != null)
            {
                WorkImageUpdated(this, new WorkImageUpdatedEventArgs(image));
            }
        }

        public FormMerging()
        {
            _option = KAConfigs.Merge;
            InitializeComponent();
        }

        public FormMerging(Bitmap bmp)
        {
            _sourceImage = bmp.Clone() as Bitmap;
            InitializeComponent();
        }

        private void InitCtrls()
        {
            if (_option == null)
            {
                return;
            }
            comboBoxMegreMode.SelectedIndex = (int)_option.MegreMode;
            OpacityScreen = _option.OpacityScreen;
            OpacityMultiply = _option.OpacityMultiply;
        }

        private void DisposeImage()
        {
            if (_sourceImage != null)
            {
                _sourceImage.Dispose();
                _sourceImage = null;
            }
            if (_destImage != null)
            {
                _destImage.Dispose();
                _destImage = null;
            }
        }

        private void OnMerging()
        {
            if (_destImage != null)
            {
                _destImage.Dispose();
                _destImage = null;
            }

            switch (comboBoxMegreMode.SelectedIndex)
            {
                case 0:
                    _destImage = ImgMerging.ScreenForGrayAndOrig(_sourceImage, _opacityScreen);
                    break;
                case 1:
                    _destImage = ImgMerging.MultiplyForOrigAndOrig(_sourceImage, _opacityMultiply);
                    break;
                case 2:
                    _destImage = ImgMerging.OnMultipleMixing(_sourceImage, _opacityScreen, _opacityMultiply);
                    break;
                default:
                    break;
            }
            OnWorkImageUpdated(_destImage.Clone() as Bitmap);           
        }
    
        private void buttonDo_Click(object sender, EventArgs e)
        {
            if (_sourceImage != null)
            {
                OnMerging();
            }
            if (_option != null)
            {
                _option.MegreMode = (ImageMegreMode)comboBoxMegreMode.SelectedIndex;
                _option.OpacityMultiply = OpacityMultiply;
                _option.OpacityScreen = OpacityScreen;
                _option.Save();
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void FormMerging_Load(object sender, EventArgs e)
        {           
            comboBoxMegreMode.SelectedIndex = _selectMode;
            if (_sourceImage == null)
            {
                buttonApply.Visible = false;
                buttonLoad.Visible = false;
            }
            else
            {
                buttonApply.Visible = true;
                buttonLoad.Visible = true;
            }
            if (_option != null)
            {
                InitCtrls();
            }
        }

        private void FormMerging_FormClosing(object sender, FormClosingEventArgs e)
        {
            WorkImageUpdated = null;
            DisposeImage();
        }

        private void textBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox box = sender as TextBox;
            int val, min = 0, max = 100;
            if (int.TryParse(box.Text, out val))
            {           
                if (val < min || val > max)
                {
                    string infoErr = string.Format("请输入一个[{0},{1}]范围内的整数.", min, max);
                    errorProvider1.SetError(box, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(box, "");
                }
            }
            else
            {
                errorProvider1.SetError(box, "不是合法的数字!");
                box.Focus();
            }
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
               || Char.IsDigit(e.KeyChar)
               || Char.IsControl(e.KeyChar)
               || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void textBoxOpacityScreen_TextChanged(object sender, EventArgs e)
        {
            OpacityScreen = int.Parse(textBoxOpacityScreen.Text);
        }

        private void trackBarOpacityScreen_Scroll(object sender, EventArgs e)
        {
            OpacityScreen = trackBarOpacityScreen.Value;
        }

        private void textBoxOpacityMultiply_TextChanged(object sender, EventArgs e)
        {
            OpacityMultiply = int.Parse(textBoxOpacityMultiply.Text);
        }

        private void trackBarOpacityMultiply_Scroll(object sender, EventArgs e)
        {
            OpacityMultiply = trackBarOpacityMultiply.Value;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            ImageMegreOption option = new ImageMegreOption();
            option.MegreMode = (ImageMegreMode)comboBoxMegreMode.SelectedIndex;
            option.OpacityMultiply = OpacityMultiply;
            option.OpacityScreen = OpacityScreen;
            option.Save();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            OnMerging();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            ImageMegreOption option = new ImageMegreOption();
            option.Load();
            comboBoxMegreMode.SelectedIndex = (int)option.MegreMode;
            OpacityScreen = option.OpacityScreen;
            OpacityMultiply = option.OpacityMultiply;
            OnMerging();
        }

    }
}
